package tPerson.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import tPerson.entity.TPerson;
import tPerson.entity.TPersonWork;
import tPerson.service.TPersonService;
import tPerson.service.TPersonWorkService;
import tPerson.util.UUIDUtil;

@Controller
@RequestMapping("/tPerson")
public class TPersonController {

    @Autowired
    private TPersonService tPersonService;

    @Autowired
    private TPersonWorkService tPersonWorkService;

    @RequestMapping(value = "/list", method = {RequestMethod.GET, RequestMethod.POST})
    public String tPersonList(@ModelAttribute("tPerson") TPerson tPerson, Model model) {
        model.addAttribute("tPersonList", tPersonService.getList(tPerson));
        return "tPerson/list";
    }

    @RequestMapping(value = "/input", method = RequestMethod.GET)
    public String update(@RequestParam(value = "personID", required = false) String tPersonID, Model model) {
        if (tPersonID != null) {
            model.addAttribute("tPerson", tPersonService.getOne(tPersonID));
            model.addAttribute("tPersonWorkList",tPersonWorkService.getList(tPersonID));
        }
        return "tPerson/input";
    }

    @RequestMapping(value = "/save",method = RequestMethod.POST)
    public String save(TPerson tPerson){
        if (tPerson.getPersonID()!=null && tPerson.getPersonID().length() > 0) {
            tPersonService.update(tPerson);
        }else {
            tPersonService.insert(tPerson);
        }
        return "redirect:list";
    }

    @RequestMapping(value = "/saveWork",method = RequestMethod.POST)
    public String save(TPersonWork tPersonWork,String[] personID){
        if (personID!=null && personID[0].length() > 0) {
            tPersonWork.setWorkID(UUIDUtil.getUUID());
            System.out.println(tPersonWork);
            tPersonWorkService.insert(tPersonWork);
        }
        return "redirect:input?personID="+personID[0];
    }

    @RequestMapping(value = "delete",method = RequestMethod.GET)
    public String delete(String personID){
        tPersonService.delete(personID);
        return "redirect:/tPerson/list";
    }

}
